package org.example.Conrtoller;

import lombok.extern.slf4j.Slf4j;
import org.example.Config.MysqlDatasource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author : zhangbo
 * @data : 2024/7/26
 * @description :
 */
@RestController
@RequestMapping("/spring")
@Slf4j
public class SpringController {
    //读取yml文件中的配置信息
    @Value("${country}")
    private String country;
    @Value("${city}")
    private String city;

    //多级读取
    @Value("${user.name}")
    private String name;

    //获取数组的某一个元素
    @Value("${likes[2]}")
    private String likes;

    @Value("${user2.hobby}")
    private String hobby;
    @Value("${user2.name}")
    private String names;

    //上面这种一一对应去映射太繁琐了，因此spring提供个一个对象
    //使用自动装配，将所有的封装到对象里面
    @Autowired
    private Environment environment;

    /**
     * 通过自动装配，获取指定的yml文件数据对象
     * @return
     */
    @Autowired
    private MysqlDatasource datasource;
    @GetMapping("/user")
    public String getName() {
        System.out.println("name:" + name);

        System.out.println("name:" + country);

        System.out.println("name:" + city);

        System.out.println("likes:" + likes);

        System.out.println(environment.getProperty("country"));
        System.out.println(environment.getProperty("user2.name"));

//        System.out.println(environment);
        System.out.println(datasource.getUrl()+datasource.getDriver()+datasource.getUsername()+datasource.getPassword());

        return "我叫:"+names+"我的爱好是:"+hobby+"=====";
    }
}
